package com.yidu.action;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.common.Pages;
import com.yidu.common.Tools;
import com.yidu.domain.ListenRawstockRecord;
import com.yidu.service.ListenRawstockRecordService;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

/**
 * @title 材料库存记录Controller类 
 * @author 江宇
 * @dateTime 2019年1月13日
 */
@Controller
@RequestMapping("/listenRawstockRecord")
public class ListenRawstockRecordController {
	@Resource
	ListenRawstockRecordService Service;
	
	/**
     * @title 查询材料库存记录
     * @param Map
     * @return List
     * @author 江宇
     * @dateTime 2018年11月29日
     * @versions 1.0
     */
	@RequestMapping("findRawstockRecord")
	@ResponseBody
	public Map<String, Object> findRawstockRecord(ListenRawstockRecord rr){
		//创建材料库存记录Map
		Map<String, Object> map=new HashMap<>();
		//如果材料库存ID不为空赋值
		if(!Tools.isEmpty(rr.getRsId())) {
			map.put("rsId", rr.getRsId());
		}
		//如果材料名称不为空赋值
		if(!Tools.isEmpty(rr.getRrName())) {
			map.put("rrName", rr.getRrName());
		}
		//如果前台传来的页数和行数不为空赋值
		if(rr.getPage()!=null && rr.getLimit()!=null) {
			Pages page=new Pages();
			page.setCurPage(rr.getPage());
			page.setMaxResult(rr.getLimit());
			
			map.put("firstRows", page.getFirstRows());
			map.put("maxResult", page.getMaxResult());
		}
		//如果开始时间不为空赋值
		if(!Tools.isEmpty(rr.getStartTime())) {
			map.put("startTime", rr.getStartTime()+" 00:00:00");
		}
		
		//如果结束时间不为空赋值
		if(!Tools.isEmpty(rr.getEndTime())) {
			map.put("endTime", rr.getEndTime()+" 23:59:59");
		}
		
		//调用查询材料库存记录的方法
		List<ListenRawstockRecord> list=Service.findRawstockRecord(map);
		//创建材料库存记录List
		List<ListenRawstockRecord> rrList=new ArrayList<>();
		for (ListenRawstockRecord listenRawstockRecord : list) {
			//创建材料库存记录类对象
			ListenRawstockRecord rr2=new ListenRawstockRecord();
			//赋值
			BeanUtils.copyProperties(listenRawstockRecord, rr2);
			//时间转换
			rr2.setRrTime2(Tools.SDF_TIME.format(listenRawstockRecord.getRrTime()));
			rrList.add(rr2);
		}
		int rows=Service.findRawstockRecordCount(map);
		
		Map<String, Object> layuiMap=new HashMap<>();
		
		layuiMap.put("code", 0);
		layuiMap.put("msg", "");
		layuiMap.put("count",rows);
		//将list放入map中
		layuiMap.put("data", rrList);
		return layuiMap;
	}
	
	/**
	 * 增加与修改
	 * @param id
	 * @param rrName
	 * @param rrTime
	 * @param rrNumber
	 * @param rrExplain
	 * @throws ParseException
	 * @author LiuXiang
	 */
	@RequestMapping("addOrUpdate")
	public void addOrUpdate(@RequestParam(value = "id")String id,@RequestParam(value = "rrName")String rrName,@RequestParam(value = "rrNumber")String rrNumber,@RequestParam(value = "rrExplain")String rrExplain){
		System.out.println("增加修改的id"+id);
		System.out.println("名字"+rrName);
		ListenRawstockRecord record = new ListenRawstockRecord();
		record.setRrId(id);
		record.setRrName(rrName);
		record.setRrNumber(rrNumber);
		record.setRrExplain(rrExplain);
		Service.addOrUpdate(record);
	}
	/**
	 * 修改上屏
	 * @param rrId
	 * @return
	 * @author LiuXiang
	 */
	@RequestMapping("/display")
	@ResponseBody
	public ListenRawstockRecord seleteById(@RequestParam(value = "id")String rrId){
		System.out.println("修改上屏======================");
		ListenRawstockRecord record = Service.selectByPrimaryKey(rrId);
		return record;
	}
	/**
	 * 删除
	 * @param rrId
	 * @return
	 * @author LiuXiang
	 */
	@RequestMapping("delete")
	public void delete(@RequestParam(value = "id")String rrId){
		
		Service.deleteByPrimaryKey(rrId);
		
	}
}

